package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.ItemCatMapper;
import com.jt.pojo.ItemCat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class ItemCatServiceImpl implements ItemCatService {
    @Autowired
    private ItemCatMapper itemCatMapper;

/*    @Override
    public List<ItemCat> findItemCatList(Integer level) {
        // 接收的 level 值不同时 执行不同的  查询方法
        long startTime = System.currentTimeMillis();
        switch (level){
            case 1: return itemCatMapper.findItemCat1();
            case 2: return itemCatMapper.findItemCat2();

        }
        List<ItemCat> itemCat3 = itemCatMapper.findItemCat3();
        long endTime = System.currentTimeMillis();
        System.out.println(endTime-startTime);
        return  itemCat3;
    }*/

    // 商品分类列表查询
    @Override
    public List<ItemCat> findItemCatList(Integer level) {
//        long startTime = System.currentTimeMillis();
        Map<Integer, List<ItemCat>> map = getMap();
        switch (level) {
            case 1:
                return map.get(0);
            case 2:
                return getTwoList(map);
        }
//        long endTime = System.currentTimeMillis();
//        System.out.println(endTime - startTime);
        return getThreeList(map);
    }

    private List<ItemCat> getThreeList(Map<Integer, List<ItemCat>> map) {
        List<ItemCat> oneList = getTwoList(map);
        for (ItemCat oneItemCat : oneList) {
            List<ItemCat> twoList = oneItemCat.getChildren();
            if (twoList == null) {
                //
                continue;
            }
            for (ItemCat twoItemCat : twoList) {
                int key = twoItemCat.getId();
                List<ItemCat> ThreeItemCatList = map.get(key);
                twoItemCat.setChildren(ThreeItemCatList);
            }
        }
        return oneList;
    }

    private List<ItemCat> getTwoList(Map<Integer, List<ItemCat>> map) {
        List<ItemCat> oneList = map.get(0);
        for (ItemCat itemCat : oneList) {
            Integer id = itemCat.getId(); // 一级的 ID 为二级的父id
            List<ItemCat> twoList = map.get(id);
            itemCat.setChildren(twoList);
        }
        return oneList;
    }

    /**
     * 将所有数据封装在一个Map集合中
     *
     * @return 所有数据的集合 按级数进行封装
     */
    private Map<Integer, List<ItemCat>> getMap() {
        Map<Integer, List<ItemCat>> map = new HashMap<>();
        List<ItemCat> itemCatList = itemCatMapper.selectList(null);
        for (ItemCat itemCat : itemCatList) {
            int key = itemCat.getParentId();
            // 如果 key 存在  就追加    key不存在就新new 一个List 然后添加数据
            if (map.containsKey(key)) {
                map.get(key).add(itemCat);
            } else {
                List<ItemCat> catList = new ArrayList<>();
                catList.add(itemCat);
                map.put(key, catList);
            }
        }
        return map;
    }

    @Override
    public void updateStatus(ItemCat itemCat) {
        itemCatMapper.updateById(itemCat);
    }

    // 商品新增操作
    @Override
    public void saveItemCat(ItemCat itemCat) {
        itemCat.setStatus(true);
        itemCatMapper.insert(itemCat);
    }

    @Override
    public void deleteItemCat(ItemCat itemCat) {
        if (itemCat.getLevel() == 3) {
            itemCatMapper.deleteById(itemCat.getId());
            return;
        }
        if (itemCat.getLevel() == 2) {
            // 获取二级的id
            int two = itemCat.getId();
            QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
            // 根据二级id 获取三级数据
            queryWrapper.eq("parent_id", two);
            // 删除三级数据
            itemCatMapper.delete(queryWrapper);
            // 然后删除二级数据
            itemCatMapper.deleteById(two);
            return;
        }
        // 获取一级id
        int one = itemCat.getId();
        QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", one);
        List<Object> twoIdList = itemCatMapper.selectObjs(queryWrapper);
        if (twoIdList.isEmpty()){
            itemCatMapper.deleteById(one);
        }else {
            queryWrapper.clear();
            queryWrapper.in("parent_id", twoIdList)
                    .or()
                    .in("id", twoIdList)
                    .or()
                    .eq("id", one);
            itemCatMapper.delete(queryWrapper);
        }
    }

    @Override
    public void updateItemCat(ItemCat itemCat) {
        itemCatMapper.updateById(itemCat);
    }
}
